local CommonSpine = class("CommonSpine")
local GameUtils = require("utils.GameUtils")
local UIUtil = require("utils.UIUtil")
local Vector3 = require("utils.Vector3")

CommonSpine.spinePos = {
    {use = false, pos = Vector3.new(100, 100, 100)},
    {use = false, pos = Vector3.new(100, 150, 100)},
    {use = false, pos = Vector3.new(100, 200, 100)},
    {use = false, pos = Vector3.new(100, 250, 100)},
    {use = false, pos = Vector3.new(100, 300, 100)},
    {use = false, pos = Vector3.new(100, 350, 100)},
    {use = false, pos = Vector3.new(100, 400, 100)},
    {use = false, pos = Vector3.new(100, 500, 100)},
    {use = false, pos = Vector3.new(100, 550, 100)},
    {use = false, pos = Vector3.new(100, 600, 100)},
    {use = false, pos = Vector3.new(150, 150, 100)},
    {use = false, pos = Vector3.new(200, 150, 100)},
    {use = false, pos = Vector3.new(250, 150, 100)},
    {use = false, pos = Vector3.new(300, 150, 100)},
    {use = false, pos = Vector3.new(350, 150, 100)},
    {use = false, pos = Vector3.new(400, 150, 100)},
    {use = false, pos = Vector3.new(450, 150, 100)},
    {use = false, pos = Vector3.new(500, 150, 100)},
    {use = false, pos = Vector3.new(550, 150, 100)}
}

function CommonSpine:ctor()
    self._heroObj = nil
    self._resId = 0
end

function CommonSpine:awake()
    printDebug("CommonSpine:awake")
end

function CommonSpine:setCallback(callback)
    self._callback = callback
end

function CommonSpine:start()
    UIUtil.addTouchEvent(
        self._heroImage,
        handler(self, self._onSpineTouchBegin),
        handler(self, self._onSpineTouchMove),
        handler(self, self._onSpineTouchEnded),
        handler(self, self._onSpineTouchCanceled)
    )
end

function CommonSpine:getCanUsePos()
    for i = 1, #CommonSpine.spinePos do
        local posData = CommonSpine.spinePos[i]
        if posData.use == false then
            posData.use = true
            return i
        end
    end
    return 0
end

function CommonSpine:updateUI(id)
    local hero = require("config.hero").get(id)
    assert(hero, "can not found cfg in her by id=" .. id)
    self:updateByResId(hero.res_id)
end

function CommonSpine:updateByResId(resId)
    if self._resId == resId then
        return
    end
    self._resId = resId
    local cfg = require("config.hero_res").get(resId)
    assert(cfg, "can not found cfg in hero_res by id=" .. resId)
    if not self._posIndex then
        self._posIndex = self:getCanUsePos()
    end
    if self._heroObj then
        GameUtils.destroy(self._heroObj)
        self._heroObj = nil
    end
    if self._posIndex > 0 then
        local pos = CommonSpine.spinePos[self._posIndex].pos
        self._heroObj = GameUtils.createUISpine(self._heroImage, cfg.res_name, pos)
    else
        self._heroObj = GameUtils.createUISpine(self._heroImage, cfg.res_name, Vector3.new(0, 0, 0))
    end

    self._spineRoot = self._heroObj.transform:GetChild(0)
    self._heroImage:SetNativeSize()
end

function CommonSpine:_onSpineTouchBegin(event)
    self._isMoved = false
end

function CommonSpine:_onSpineTouchMove(event)
    local pos = event.position
    local delta = event.delta
    if self._heroObj then
        local eulerAngles = self._spineRoot.eulerAngles
        eulerAngles.y = eulerAngles.y - delta.x
        self._isMoved = true
        self._spineRoot.eulerAngles = eulerAngles
    end
end

function CommonSpine:_onSpineTouchEnded(event)
    if self._callback and not self._isMoved then
        self._callback()
    end
end

function CommonSpine:_onSpineTouchCanceled(event)
end

function CommonSpine:onDestroy()
    GameUtils.destroy(self._heroObj)
    if self._posIndex and self._posIndex > 0 then
        CommonSpine.spinePos[self._posIndex].use = false
    end
end

return CommonSpine
